﻿#region Directives

using System;
using System.Web;
using System.Web.SessionState;
using System.Globalization;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using Astro.Core;
using System.Net;
using DataBuddy;
using System.IO;

#endregion

namespace Astro.Plus
{
    [Chalk("helper")]
    public class HelperExtensions
    {
        public string SeparatorFormat(object val)
        {
            if (val != null)
            {
                try
                {
                    Int64 temp = 0;
                    Int64.TryParse(val.ToString(), out temp);
                    return temp.ToString("#,####,###,###", CultureInfo.InvariantCulture);
                }
                catch { }
            }

            return val as string;
        }

        public string ClearNull(string val)
        {
            if (val == null)
                return string.Empty;
            else
                return val;
        }

        public string[] GetFiles(string path)
        {
            string themePath = ViewManager.GetAbsoluateFilePath(AstroContext.Current.Theme, path);
            string fullPath = ViewManager.GetFilePath(AstroContext.Current.Theme, path);
            List<string> results = new List<string>();
            foreach (string file in Directory.GetFiles(fullPath))
            {
                FileInfo f = new FileInfo(file);
                results.Add(themePath + "/" + f.Name);
            }

            return results.ToArray();
        }

        public string GetQueryString(string keyName)
        {
            return HttpContext.Current.Request.QueryString[keyName];
        }

        public void RemoveSessionItem(string keyName)
        {
            HttpContext.Current.Session.Remove(keyName);
        }

        public string GetPermalink()
        {
            return HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri);
        }
        public void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url);
        }
    }
}